#include "mainwindow.h"
#include "ui_mainwindow.h"
#include "gwrectangle.h"
#include "gwdetector.h"
#include "undocommand.h"

#include <QDebug>
#include <QScrollBar>



MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
    , ui(new Ui::MainWindow),m_undoStack(new QUndoStack(this))
{
    ui->setupUi(this);
    view=new QGraphicsView(ui->centralwidget);
    view->setAutoFillBackground(true);
    this->createNewScene();

//    GwRectangle *rect=new GwRectangle();
//    rect->setName("test1");
//    scene->addItem(rect);

//    GwDetector *dec=new GwDetector();
//    dec->setConfig();
//    scene->addItem(dec);

//    QGraphicsTextItem *txt=new QGraphicsTextItem();
//    QFont txtfont;
//    txtfont.setPointSize(20);
//    txt->setPlainText("test");
//    txt->setFont(txtfont);
//    txt->setFlags(QGraphicsItem::ItemIsSelectable | QGraphicsItem::ItemIsMovable);
//    scene->addItem(txt);

    ui->vLay->addWidget(view);
    QScrollBar *pHbar =  view->horizontalScrollBar();
    pHbar->setSliderPosition(1);
    QScrollBar *pVbar = view->verticalScrollBar();
    pVbar->setSliderPosition(1);

    connect(ui->file_new,&QAction::triggered,this,&MainWindow::newfile);

//    connect(this,&MainWindow::setGraphicsState,rect,&GwRectangle::setState);

    propertyInit(ui->treeProperty);

    this->showMaximized();

}

MainWindow::~MainWindow()
{
//    qDebug()<<"begin del";
//    delete view;
//    view->deleteLater();
//    delete scene;
//    delete ui;
//    ui=NULL;
    qDebug()<<"after del";
}

void MainWindow::refreshProperty(QtProperty *item)
{
    if(item->propertyName()=="宽度")
        m_pVarManager->setValue(item,scene->width());
//        item->setValue(scene->width());
    else if(item->propertyName()=="高度")
        m_pVarManager->setValue(item,scene->height());
//        item->setValue(scene->height());
    else if(item->propertyName()=="背景图")
    {
        this->backgroundFile=scene->backgroundFile;
        m_stringManage->setValue(item,backgroundFile);
    }
}

void MainWindow::propertyInit(QtTreePropertyBrowser *treepro)
{
    if(m_pVarManager==NULL)
        m_pVarManager =new QtVariantPropertyManager();
    if(m_pVarFactory==NULL)
        m_pVarFactory =new QtVariantEditorFactory();
    if(m_stringManage==NULL)
        m_stringManage=new QtStringPropertyManager();
    if(m_stringFactory==NULL)
        m_stringFactory=new fileEditFactory();

    m_propertys.clear();

    treepro->setAlternatingRowColors(true);
    treepro->clear();

    connect(m_pVarManager,&QtVariantPropertyManager::valueChanged,this,&MainWindow::propertyChanged);
    treepro->setFactoryForManager(m_pVarManager,m_pVarFactory);


    QtVariantProperty *item =m_pVarManager->addProperty(QVariant::Int, QStringLiteral("宽度"));
    m_property_dic[item] =QStringLiteral("宽度");
    m_propertys.append(item);
    item->setValue(700);
    treepro->addProperty(item);


    item =m_pVarManager->addProperty(QVariant::Int, QStringLiteral("高度"));
    m_property_dic[item] =QStringLiteral("高度");
    m_propertys.append(item);
    item->setValue(700);
    treepro->addProperty(item);

    treepro->setFactoryForManager(m_stringManage,m_stringFactory);
//    treepro->setFactoryForManager(m_stringManage,m_stringFactory);
    connect(m_stringManage,&QtStringPropertyManager::valueChanged,
            this,&MainWindow::propertyQStringChanged);

    QtProperty *it=m_stringManage->addProperty(QStringLiteral("背景图"));
    m_stringManage->setValue(it,backgroundFile);
    m_propertys.append(it);
    treepro->addProperty(it);

}

void MainWindow::setPropertyTree(QtTreePropertyBrowser *treepro)
{
    QtProperty *item;
    if(treepro==NULL)
        return;
    treepro->clear();
    foreach(item, m_propertys)
    {
        this->refreshProperty(item);
        treepro->addProperty(item);
    }
    treepro->setFactoryForManager(m_pVarManager,m_pVarFactory);

//    m_stringManage->setValue(m_stringManage->property());
    treepro->setFactoryForManager(m_stringManage,m_stringFactory);
//    m_stringManage->setValue();

}

void MainWindow::createNewScene()
{
    if(this->scene!=nullptr)
    {
        this->scene->deleteLater();
        this->scene=nullptr;
    }
    this->scene=new GwScene(view);
    scene->setSceneRect(0,0,1024,768);
    view->setScene(scene);
    connect(scene,&QGraphicsScene::selectionChanged,this,&MainWindow::sceneSelectionChanged);
    connect(ui->file_save,&QAction::triggered,scene,&GwScene::save);
}

void MainWindow::newfile(bool checked)
{

}

void MainWindow::savefile(bool checked)
{


}

void MainWindow::propertyChanged(QtProperty *property, const QVariant &val)
{
    if(property->propertyName()=="宽度"){
        scene->setSceneRect(0,0,val.toInt(),scene->height());
    }
    if(property->propertyName()=="高度"){
        scene->setSceneRect(0,0,scene->width(),val.toInt());
    }
    view->resize(scene->width()+2,scene->height()+2);

}

void MainWindow::propertyQStringChanged(QtProperty *property, const QVariant &val)
{

    if(ui==NULL)
        return;
    scene->setSceneRect(0,0,view->width()-2,view->height()-2);
    scene->setBackgroundPic(val.toString());
    ui->treeProperty->setCurrentItem(NULL);
    backgroundFile=val.toString();
    sceneSelectionChanged();
    scene->update();
}

void MainWindow::sceneSelectionChanged()
{
    if(ui==NULL)
        return;
    if(scene->selectedItems().count()>0)
    {
        if(typeid(*(scene->selectedItems()[0])).hash_code()==typeid(GwRectangle).hash_code())
        {
            GwRectangle *gwitem;
            gwitem=static_cast<GwRectangle *>(scene->selectedItems()[0]);
            gwitem->setPropertyTree(ui->treeProperty);
        }
        else if(typeid(*(scene->selectedItems()[0])).hash_code()==typeid(GwDetector).hash_code())
        {
            GwDetector *gwitem;
            gwitem=static_cast<GwDetector *>(scene->selectedItems()[0]);
            gwitem->setPropertyTree(ui->treeProperty);
        }
        else if(typeid(*(scene->selectedItems()[0])).hash_code()==typeid(GwText).hash_code())
        {
            GwText *gwitem;
            gwitem=static_cast<GwText *>(scene->selectedItems()[0]);
            gwitem->setPropertyTree(ui->treeProperty);
        }
    }
    else
    {
        qDebug()<<ui->treeProperty;
        this->setPropertyTree(ui->treeProperty);
    }
}


void MainWindow::on_rect_new_triggered()
{
    m_undoStack->push(new AddRoomCommand(this->scene));
}

void MainWindow::on_undo_triggered()
{
    m_undoStack->undo();
}

void MainWindow::on_redo_triggered()
{
    m_undoStack->redo();
}

void MainWindow::on_border_top_triggered()
{
    qreal topPt=scene->selectedItems()[0]->pos().y();
    QGraphicsItem *item;
    foreach(item,scene->selectedItems())
    {
        if(item->pos().y()<topPt)
        {
            topPt=(item->pos()).y();
        }
    }
    foreach(item,scene->selectedItems())
    {
        item->setPos(item->pos().x(),topPt);
    }
}

void MainWindow::on_border_bottom_triggered()
{
    QGraphicsItem *fstitem=scene->selectedItems()[0];
    qreal bottomPt=fstitem->pos().y()+fstitem->boundingRect().height();
    QGraphicsItem *item;
    foreach(item,scene->selectedItems())
    {
        if(item->pos().y()>bottomPt)
        {
            bottomPt=(item->pos()).y()+item->boundingRect().height();
        }
    }
    foreach(item,scene->selectedItems())
    {
        item->setPos(item->pos().x(),bottomPt-item->boundingRect().height());
    }
}

void MainWindow::on_align_left_triggered()
{
    qreal leftPt=scene->selectedItems()[0]->pos().x();
    QGraphicsItem *item;
    foreach(item,scene->selectedItems())
    {
        if(item->pos().x()<leftPt)
        {
            leftPt=(item->pos()).x();
        }
    }
    foreach(item,scene->selectedItems())
    {
        item->setPos(leftPt,item->pos().y());
    }
}

void MainWindow::on_align_right_triggered()
{
    QGraphicsItem *fstitem=scene->selectedItems()[0];
    qreal rightPt=fstitem->pos().x()+fstitem->boundingRect().width();
    QGraphicsItem *item;
    foreach(item,scene->selectedItems())
    {
        if(item->pos().x()>rightPt)
        {
            rightPt=(item->pos()).x()+item->boundingRect().width();
        }
    }
    foreach(item,scene->selectedItems())
    {
        item->setPos(rightPt-item->boundingRect().width(),item->pos().y());
    }
}


void MainWindow::resizeEvent(QResizeEvent *event)
{
    QTransform tf;
    qreal sx,sy;
    if(scene!=NULL)
    {
        if(scene->width()<view->width())
            scene->setSceneRect(0,0,view->width(),scene->height());
        if(scene->height()<view->height())
            scene->setSceneRect(0,0,scene->width(),view->height());
        sceneSelectionChanged();
    }
    QMainWindow::resizeEvent(event);
}


void MainWindow::on_file_open_triggered()
{
    this->createNewScene();
    scene->load(true);
    sceneSelectionChanged();
}

void MainWindow::on_file_new_triggered()
{
    this->createNewScene();
}

void MainWindow::on_ds_new_triggered()
{
    m_undoStack->push(new AddDetectorCommand(this->scene));
}

void MainWindow::on_string_new_triggered()
{
    m_undoStack->push(new AddStringCommand(this->scene));
}
